package com.lowca.news.dao;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

/**
 * Base class for running DAO tests.
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
@ContextConfiguration(locations = { "classpath:/spring.xml" })
public abstract class BaseDaoTestCase extends
		AbstractTransactionalJUnit4SpringContextTests {

	    @Autowired
	    private SessionFactory sessionFactory;

	    /**
	     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
	     */
	    protected final Log log = LogFactory.getLog(getClass());
	    /**
	     * ResourceBundle loaded from src/test/resources/${package.name}/ClassName.properties (if exists)
	     */
	    protected ResourceBundle rb;

	    /**
	     * Default constructor - populates "rb" variable if properties file exists for the class in
	     * src/test/resources.
	     */
	    public BaseDaoTestCase() {
	        // Since a ResourceBundle is not required for each class, just
	        // do a simple check to see if one exists
	        String className = this.getClass().getName();

	        try {
	            rb = ResourceBundle.getBundle(className);
	        } catch (MissingResourceException mre) {
	            log.trace("No resource bundle found for: " + className);
	        }
	    }

	    /**
	     * Create a HibernateTemplate from the SessionFactory and call flush() and clear() on it.
	     * Designed to be used after "save" methods in tests: http://issues.appfuse.org/browse/APF-178.
	     *
	     * @throws org.springframework.beans.BeansException
	     *          when can't find 'sessionFactory' bean
	     */
	    protected void flush() throws BeansException {
	        HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
	        hibernateTemplate.flush();
	        hibernateTemplate.clear();
	    }


}
